package com.heima.homeservicemanagementsystem.controller;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.heima.homeservicemanagementsystem.entity.AccountSettings;
import com.heima.homeservicemanagementsystem.entity.Employees;
import com.heima.homeservicemanagementsystem.service.EmployeesService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import com.heima.homeservicemanagementsystem.module.PageParams;
import com.heima.homeservicemanagementsystem.module.Result;
import jakarta.annotation.Resource;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import com.heima.homeservicemanagementsystem.entity.QualificationReview;
import com.heima.homeservicemanagementsystem.service.QualificationReviewService;
import com.heima.homeservicemanagementsystem.mapper.QualificationReviewMapper;

import java.util.List;
import java.util.Map;
import java.util.Objects;

import static cn.dev33.satoken.SaManager.log;

/**
 * 资质审核;(qualification_review)表控制层
 *
 * @author : http://www.chiner.pro
 * @date : 2025-4-24
 */
@Tag(name = "资质审核对象功能接口")
@RestController
@RequestMapping("/qualificationReview")
public class QualificationReviewController {

    @Resource
    private QualificationReviewService qualificationReviewService;

    @Resource
    private QualificationReviewMapper qualificationReviewMapper;
    @Resource
    private EmployeesService employeesService;

    @Operation(summary = "分页查询")
    @PostMapping("/page")
    public Result<Page<QualificationReview>> paginQuery(@RequestBody PageParams<QualificationReview> pageParams) {
        //查询参数
        QualificationReview params = pageParams.getParams();
        LambdaQueryWrapper<QualificationReview> lqw = new LambdaQueryWrapper<>();


        lqw.eq(params.getUserId() != null, QualificationReview::getUserId, params.getUserId());


        lqw.eq(params.getName() != null, QualificationReview::getName, params.getName());


        lqw.eq(params.getIdentityCard() != null, QualificationReview::getIdentityCard, params.getIdentityCard());


        lqw.eq(params.getExamine() != null, QualificationReview::getExamine, params.getExamine());


        lqw.eq(params.getPass() != null, QualificationReview::getPass, params.getPass());


        return Result.success(qualificationReviewService.page(pageParams.getPage(), lqw));
    }


    @Operation(summary = "资质审核所有搜索")
    @GetMapping("/list")
    public Result searchQuery() {
        List<QualificationReview> list = qualificationReviewService.list();
        for (QualificationReview accountSettings : list) {
            Employees one = employeesService.getOne(Wrappers.<Employees>lambdaQuery().eq(Employees::getId, accountSettings.getUserId()));
            one.setPassword("");
            one.setSlat("");
            accountSettings.setEmployees(one);
        }


        return Result.success(list);
    }

    @Operation(summary = "有条件的搜索")
    @PostMapping("/tjSearch")
    public Result Query(@RequestBody QualificationReview qualificationReview) {
        LambdaQueryWrapper<QualificationReview> lqw = new LambdaQueryWrapper<>();


        lqw.eq(qualificationReview.getId() != null, QualificationReview::getId, qualificationReview.getId());


        lqw.eq(qualificationReview.getUserId() != null, QualificationReview::getUserId, qualificationReview.getUserId());


        lqw.like(qualificationReview.getName() != null, QualificationReview::getName, qualificationReview.getName());


        lqw.eq(qualificationReview.getIdentityCard() != null, QualificationReview::getIdentityCard, qualificationReview.getIdentityCard());


        lqw.eq(qualificationReview.getExamine() != null, QualificationReview::getExamine, qualificationReview.getExamine());


        lqw.eq(qualificationReview.getPass() != null, QualificationReview::getPass, qualificationReview.getPass());


        return Result.success(qualificationReviewService.list(lqw));
    }

    /**
     * 新增数据
     *
     * @param qualificationReview 实例对象
     * @return 实例对象
     */
    @Operation(summary = "新增数据")
    @PostMapping("/insert")
    public Result insert(@RequestBody QualificationReview qualificationReview) {
        boolean exists = qualificationReviewService.exists(Wrappers.<QualificationReview>lambdaQuery().eq(QualificationReview::getUserId, qualificationReview.getUserId()));
        if (exists) {
            qualificationReview.setExamine("false");
            qualificationReview.setPass("false");
            qualificationReview.setSuggestedModifications("");
            qualificationReviewService.updateById(qualificationReview);
            return Result.success("该用户已申请");
        }
        qualificationReview.setExamine("false");
        qualificationReview.setPass("false");
        qualificationReview.setSuggestedModifications("");
        if (qualificationReviewService.save(qualificationReview)) {
            return Result.success();
        }
        return Result.error();
    }
    @Operation(summary = "通过用户名字查询")
    @PostMapping("/queryByid")
    public Result queryByid(@RequestBody Map<Object, String> userId) {
        log.info("userId:" + userId);
        String userId1 = userId.get("userId");
        QualificationReview one = qualificationReviewService.getOne(Wrappers.<QualificationReview>lambdaQuery().eq(QualificationReview::getUserId, userId1));
        if (Objects.nonNull(one)) {
            return Result.success(one);
        }

        return Result.error();
    }
    @Operation(summary = "后台的反馈信息")

    @PutMapping("/htupdate")
    public Result<Boolean> htupdate(@RequestBody QualificationReview qualificationReview) {
        log.info("信息" + qualificationReview);
        LambdaUpdateWrapper<QualificationReview> objectUpdateWrapper = new LambdaUpdateWrapper<>();

        objectUpdateWrapper.set(StringUtils.hasLength(qualificationReview.getSuggestedModifications()), QualificationReview::getSuggestedModifications, qualificationReview.getSuggestedModifications());
        objectUpdateWrapper.set(StringUtils.hasLength(qualificationReview.getExamine()), QualificationReview::getExamine, qualificationReview.getExamine());
        objectUpdateWrapper.set(StringUtils.hasLength(qualificationReview.getPass()), QualificationReview::getPass, qualificationReview.getPass());

        objectUpdateWrapper.eq(QualificationReview::getId, qualificationReview.getId());

        if (qualificationReviewMapper.update(null, objectUpdateWrapper) > 0) {
            return Result.success();
        }
        return Result.error();
    }
    /**
     * 更新数据
     *
     * @param qualificationReview 实例对象
     * @return 实例对象
     */
    @Operation(summary = "更新数据")
    @PutMapping("update")
    public Result<Boolean> update(@RequestBody QualificationReview qualificationReview) {
        if (qualificationReviewService.updateById(qualificationReview)) {
            return Result.success();
        }
        return Result.error();
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Operation(summary = "通过主键删除数据",
            parameters = {
                    @Parameter(name = "id", description = "主键", required = true, in = ParameterIn.PATH)
            })
    @DeleteMapping("/delete/{id}")
    public Result<Boolean> deleteById(@PathVariable String id) {

        if (qualificationReviewService.removeById(id)) {
            return Result.success();
        }
        return Result.error();
    }

    @Operation(summary = "多id删除数据")
    @DeleteMapping("/delete")
    public Result<Boolean> deleteByIds(@RequestBody List<String> id) {
        log.info("前端信息：{}", id);
        if (qualificationReviewService.removeByIds(id)) {
            return Result.success();
        }
        return Result.error();
    }
}